<?php
include 'header.php';

@$id = $_REQUEST ['id'];
@$message = empty($id)?"":check($id);
function check ($id){
	@$code = $_REQUEST['code'];
	@$login = $_REQUEST ['login'];
	@$password = $_REQUEST ['password'];
	@$newPwd = $_REQUEST ['newPwd'];
	@$newPwd2 = $_REQUEST ['newPwd2'];
	
	if(empty($code)||empty($login)||empty($password)||empty($newPwd)||empty($newPwd2)){
		return $message = '都必须填写';
	}
	include 'db/code.php';
	if(!checkCode($code)){
		return $message = '验证码错误';
	}
	@$user = dbSelect('select id,password from user where login=:login',array('login'=>$login));
	if (empty ( $user )) {
		return $message = "原帐号错误";
	}
	if ($user ['password'] != $password) {
		return $message = "原密码错误";
	}
	if ($newPwd != $newPwd2) {
		return $message = "两次新密码输入不一致";
	}
	@$flog = dbUpdate('update user set password=:pwd where id=:id',array('pwd'=>$newPwd,'id'=>$id));
	if ($flog) {
		unset ( $_SESSION );
		header ( 'Refresh:0;url=/changeLogOk.php' );
		die();
	} else {
		return $message= '修改失败';
	}
}
?>
   		<div class='container'>
   			<div class='row'>
   				<div class='col-xs-2'>
   					<a href='index.php' class='btn btn-default btn-block'>返回首页</a>
   				</div>
   				<div class='col-xs-4'>
   					<div class='panel panel-primary'>
   						<div class='panel-heading'>
   							<div class='panel-title'>修改密码</div>
   						</div>
   						<form class='form-horizontal' action='changeLogin.php' method='post'>
	   						<div class='panel-body'>
	   							<?php if(!empty($message)){?>
									<div class="alert alert-danger">
										<?php @print($message);?>
		                   				<div class="close" data-dismiss="alert">&times;</div>
									</div>
								<?php }?>
								<input type='hidden' name="id" value="<?php echo $_SESSION['userId'];?>">
								<div class="form-group <?php echo @$message=="帐号错误"?' has-error':'';?>">
									<label for="login" class="col-xs-4 control-label">登录名</label>
									<div class="col-xs-8">
										<input class="form-control" id="login" placeholder="登录名" name="login" type="text" required="required" >
									</div>
								</div>
								<div class="form-group <?php echo @$message=="密码错误"?' has-error':'';?>">
									<label for="password" class="col-xs-4 control-label">密码</label>
									<div class="col-xs-8">
										<input class="form-control" id="password" placeholder="密码" name="password" type="password" required="required">
									</div>
								</div>
								<div class="form-group <?php echo @$message=="两次新密码输入不一致"?' has-error':'';?>">
									<label for="newPwd" class="col-xs-4 control-label">新密码</label>
									<div class="col-xs-8">
										<input class="form-control" id="newPwd" placeholder="新密码" name="newPwd" type="password" required="required">
									</div>
								</div>
								<div class="form-group <?php echo @$message=="两次新密码输入不一致"?' has-error':'';?>">
									<label for="newPwd2" class="col-xs-4 control-label">确认新密码</label>
									<div class="col-xs-8">
										<input class="form-control" id="newPwd2" placeholder="确认" name="newPwd2" type="password" required="required">
									</div>
								</div>
								<div class='form-group'>
	   								<div class='col-xs-4 col-xs-offset-4'>
		   								<img src='img.php' class='codeImg' />
	   								</div>
	   								<a href='#' class='nextCode'>看不清，换一张</a>
	   							</div>
	   							<div class='form-group <?php echo @$message=="验证码错误"?'has-error':'';?>'>
	   								<label class='col-xs-4 control-label' for='code'>验证码</label>
	   								<div class='col-xs-8'>
		   								<input class='form-control' name='code' id='code' placeholder='验证码' required='required' />
	   								</div>
	   							</div>
								<div class='row'>
		   							<div class='col-xs-2 col-xs-offset-8'>
		   								<button class='btn btn-primary' type='submit' style='width:70px;'>确定</button>
		   							</div>
		   						</div>
	   						</div>
   						</form>
   					</div>
   				</div>
   			</div>
   		</div>
      <script src='bootstrap/js/nextImg.js'></script>
   	</body>
</html>